namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class UserAttrBLL
    {
        public int Add(UserAttr model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserAttr(");
            builder.Append("AttributeName,AttributeNameCN,GroupID");
            builder.Append(") values (");
            builder.Append("@AttributeName,@AttributeNameCN,@GroupID");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AttributeName", model.AttributeName), new SqlParameter("@AttributeNameCN", model.AttributeNameCN), new SqlParameter("@GroupID", model.GroupID) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int AttributeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserAttr ");
            builder.Append(" where AttributeID=@AttributeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AttributeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AttributeID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string AttributeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserAttr ");
            builder.Append(" where ID in (" + AttributeIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<UserAttr> GetList(string whereStr = "")
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserAttr ");
            builder.Append(" WHERE 1=1 ");
            builder.Append(" AND " + whereStr);
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            UserAttr item = null;
            List<UserAttr> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserAttr>();
                foreach (DataRow row in table.Rows)
                {
                    item = new UserAttr();
                    if (row["AttributeID"].ToString() != "")
                    {
                        item.AttributeID = int.Parse(row["AttributeID"].ToString());
                    }
                    item.AttributeName = row["AttributeName"].ToString();
                    item.AttributeNameCN = row["AttributeNameCN"].ToString();
                    if (row["GroupID"].ToString() != "")
                    {
                        item.GroupID = int.Parse(row["GroupID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public UserAttr GetModel(int AttributeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select AttributeID, AttributeName, AttributeNameCN, GroupID  ");
            builder.Append("  from UserAttr ");
            builder.Append(" where AttributeID=@AttributeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AttributeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AttributeID;
            UserAttr attr = new UserAttr();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["AttributeID"].ToString() != "")
                {
                    attr.AttributeID = int.Parse(row["AttributeID"].ToString());
                }
                attr.AttributeName = row["AttributeName"].ToString();
                attr.AttributeNameCN = row["AttributeNameCN"].ToString();
                if (row["GroupID"].ToString() != "")
                {
                    attr.GroupID = int.Parse(row["GroupID"].ToString());
                }
                return attr;
            }
            return null;
        }

        public bool Update(UserAttr model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserAttr set ");
            builder.Append(" AttributeName = @AttributeName , ");
            builder.Append(" AttributeNameCN = @AttributeNameCN , ");
            builder.Append(" GroupID = @GroupID  ");
            builder.Append(" where AttributeID=@AttributeID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AttributeID", model.AttributeID), new SqlParameter("@AttributeName", model.AttributeName), new SqlParameter("@AttributeNameCN", model.AttributeNameCN), new SqlParameter("@GroupID", model.GroupID) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

